import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    private GreenfootImage image1=null;
    private GreenfootImage image2=null;
    int speed=0,score=0;
    boolean flag=false,win=false;
    public void act()
    {
        key();
        turnAtEdge();
        start();
        animate();
        findAndEat();
    }
    public void findAndEat()
    {
        if(isTouching(Worm.class))
        {
            removeTouching(Worm.class);
            score++;
            Greenfoot.playSound("slurp.wav");
            if(score==19)
            {
                win=true;
                speed=0;
                Greenfoot.playSound("fanfare.wav");
            }
        }
    }
    public void LobsterEat()
    {
        if(isTouching(Lobster.class))
        {
            removeTouching(Crab.class);
            speed=0;
            Greenfoot.playSound("au.wav");
        }
    }
    public void key()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(3);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(-3);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            speed+=greenfoot.Greenfoot.getRandomNumber(3);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            speed/=2;
        }
    }
    public void start()
    {
        if(speed>=5&&!flag)
        {
            move(speed);
            flag=true;
        }
        else if(flag)
        {
            move(speed);
        }
        else
        {
            move(0);
        }
    }
    public void turnAtEdge()
    {
        if(isAtEdge()||win==true)
        {
            turn(33);
        }
    }
    public Crab()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
    }
    public void animate()
    {
        if(getImage()==image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
}


